<template>
	<view>
		<!-- addredd -->
		<view>
			<view style="padding: 15px;border: 1px solid;" @click="chooseAddress">
				<h4>{{defAddress.address}}</h4>
				<view style="font-size: 14px;margin-top: 9px;">
					<text>{{defAddress.name}} </text>
					<text>{{defAddress.phone}}</text>
				</view>
			</view>
		</view>
		<!-- goods -->
		<view style="margin-top: 10px;">
			<view  v-for="(item, index) in goodsList">
				<view style="padding: 15px;" >
						<!-- 图片 -->
						<image :src="item.goods.cover_url" style="width: 200rpx;height: 200rpx;float: left;"></image>
						<view style="display: inline-block;height: 100%;">
							<text style="font-size: 14px;font-weight: 600;">{{item.goods.title}}</text>
							<br>
							<text style="font-size: 6px;font-weight: 90;color: #ababa9;">{{item.goods.description}}</text>
							<view style="padding-top: 30px;">
								<text style="color: red;">￥ {{item.goods.price}}</text>
								<view style="display: inline-block;float: right;">
									<text>x {{item.num}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
		</view>
		
		<!-- footer -->
		<view style="bottom:20px;position: fixed;width: 100%;padding-left: 10px;margin-right: 10px;" >
			<view style="display: inline-block;float: right;">
				<text>合计:</text>
				<text style="color: red;">￥{{totalPrice}}</text>
				<view  class="right" >
					<view class="buy btn u-line-1" >提交并支付</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList:[],
				address:[],
				defAddress:{},
			}
		},
		onLoad() {
			this.getData()
		},
		onShow() {
			if(uni.getStorageSync('address')){
				this.defAddress = uni.getStorageSync('address')
			}
		},
		computed:{
			totalPrice(){
				return this.goodsList.reduce((pre,item)=>{
				  if(item.is_checked==1){
						pre = pre + item.goods.price * item.num
					}
					return pre
				},0)
			}
		},
		methods: {
			//获取商品
			async getData(){
				let res = await this.$u.api.orderPre()
				this.goodsList = res.carts
				this.address = res.address
				if(uni.getStorageSync('address')){
					this.defAddress = uni.getStorageSync('address')
				}else{
					this.address.forEach(item=>{
						if(item.is_default===1){
							this.defAddress = item
						}
					})
				}
			
			},
			//选择地址
			chooseAddress(){
				this.$u.route({
					url:'pages/cart/addredd'
				})
			}
		}
	}
</script>

<style lang="scss">
.right {
			display: flex;
			float: right;
			right: 0;
			font-size: 28rpx;
			align-items: center;
			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
			}
			.cart {
				background-color: #ed3f14;
				margin-right: 30rpx;
			}
			.buy {
				margin-top: -10px;
				margin-right: 15px;
				left: 10px;
				background-color: #ff7900;
			}
		}
</style>
